﻿//*********************************************************************
//
//  文件名：ShortMessageController
//
//  作者： LW
//
//  作用：
//
//  日期：2015/9/28 11:22:14
//
//*********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using YouTravel.DomainService;
using Framework.Common;
using YouTravel.DomainModels;
using YouTravel.Framework.Enums;
using YouTravel.Framework.ResourceMessage;

namespace Application.WebAPI.Controllers
{
    /// <summary>
    /// ShortMessageController
    /// 短信
    /// </summary>
    [RoutePrefix("api/shortmessage")]
    public class ShortMessageController : ApiController
    {
        IShortMessageService shortMessageService;
        IUserService userService;
        DataResult dataResult = new DataResult() { Code = ResponseStatusCode.Error, Msg = "" };

        public ShortMessageController(IShortMessageService _shortMessageService, IUserService _userService)
        {
            this.shortMessageService = _shortMessageService;
            this.userService = _userService;
        }

        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet, Route("sendvcode"), AllowAnonymous]
        public DataResult SendVCode(string phone)
        {
            bool flag = this.userService.CheckPhone(phone);
            if (flag)
            {
                string vcode = CommonMethod.GetInstance.GenerateRandomCode(6);
                ShortMessage model = new ShortMessage()
                {
                    Code = vcode,
                    Content = "您正在找回密码，验证码为：" + vcode + "，请不要告诉他人哟",
                    Receive = phone,
                    Status = ShortMessageStatus.Normal,
                    Type = ShortMessageType.RetrievePWD
                };
                this.shortMessageService.SendMsg(model);

                dataResult.Code = ResponseStatusCode.Success;
                dataResult.Msg = BusinessResourceMessage.Success;
                //dataResult.Data = vcode;
            }
            return dataResult;
        }

        /// <summary>
        /// 验证码是否正确
        /// </summary>
        /// <param name="vcode"></param>
        /// <returns></returns>
        [HttpGet, Route("validatevcode"), AllowAnonymous]
        public DataResult ValidateVCode([FromUri]string vcode)
        {
            DateTime dt = DateTime.Now.AddMinutes(-3);
            bool flag = this.shortMessageService.ValidateVCode(vcode, dt);
            if (flag)
            {
                dataResult.Code = ResponseStatusCode.Success;
                dataResult.Msg = BusinessResourceMessage.Success;
            }
            else
            {
                dataResult.Code = ResponseStatusCode.Error;
                dataResult.Msg = "验证码不正确";
            }

            return dataResult;
        }
    }
}